package com.cn.aam.checaiduo.ui.more;

import android.os.CountDownTimer;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.cn.aam.checaiduo.Const;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.backend.RetrofitHome;
import com.cn.aam.checaiduo.backend.observer.OnRequestListener;
import com.cn.aam.checaiduo.backend.rx.RxSchedulers;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.base.ToolBarManager;
import com.cn.aam.checaiduo.bean.base.BaseResponseB;
import com.cn.aam.checaiduo.ui.arouter.UIRouter;
import com.cn.aam.checaiduo.ui.arouter.section.RouterMore;
import com.cn.aam.checaiduo.utils.AuthUtil;
import com.cn.aam.checaiduo.utils.MyEditTextUtils;
import com.cn.aam.checaiduo.widget.WMToast;

import butterknife.BindView;
import butterknife.OnClick;


@Route(path = UIRouter.MORE.CHANGE_MOBILE_STEP1, name = "修改手机号码第1步")
public class ChangeMobileStep01Activity extends BaseActivity {
    public static final String DATA_MOBILE = "data_mobile";

    @BindView(R.id.tv_mobile)
    TextView tvMobile;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn_code)
    TextView btnCode;


    private CountDownTimer cd;
    private boolean getCodeIng = false;//正在获取验证码中
    private String mobile;
    private String code;


    @Override
    protected int layoutId() {
        return R.layout.activity_change_mobile_step01;
    }

    @Override
    protected void onGenerate() {
        mobile = getIntent().getStringExtra(DATA_MOBILE);
    }

    @Override
    protected void initView() {
        //toolbar
        ToolBarManager.with(this, WMContent)
                .setBackgroundColor(R.color.toolbar_bg_grey)
                .setNavigationIcon(R.drawable.ic_nav_arrow_back)
                .setTitle("更改")
                .setTitleColor(R.color.black_normal);
        //mobile
        tvMobile.setText(AuthUtil.hideMobileNo(mobile));
    }


    /**
     * 获取验证码
     */
    @OnClick(R.id.btn_code)
    public void onClickGetCode() {
        RetrofitHome.getInstance()
                .getInternalApi()
                .getSMSCode(mobile)
                .compose(RxSchedulers.<BaseResponseB>toMain())
                .subscribe(new OnRequestListener<BaseResponseB>(this) {
                    @Override
                    public void onNext(BaseResponseB value) {
                        super.onNext(value);
                        if (value.getCode() == Const.REQUEST_SUCCESS)
                            startCountTime();
                    }
                });
    }

    /**
     * 下一步
     */
    @OnClick(R.id.btn_next)
    public void onClickNext() {
        if (!CVBSubmit()) return;
        RetrofitHome.getInstance()
                .getInternalApi()
                .verifyOldPhoneSmsCode(code)
                .compose(RxSchedulers.<BaseResponseB>toMain())
                .subscribe(new OnRequestListener<BaseResponseB>(this) {
                    @Override
                    public void onNext(BaseResponseB value) {
                        super.onNext(value);
                        if (value.getCode() == Const.REQUEST_SUCCESS) {
                            onBackPressed();
                            UIRouter.getInstance().getMore().openChangeMobileStep02(mContext, mobile);
                        }
                    }
                });

    }


    //check value before submit
    public boolean CVBSubmit() {
        code = MyEditTextUtils.getContent(etCode);
        if (!getCodeIng) {
            WMToast.normal("请获取验证码");
            return false;
        }
        if (TextUtils.isEmpty(code)) {
            WMToast.normal("请输入验证码");
            return false;
        }
        return true;
    }


    /**
     * 开始倒计时
     */
    private void startCountTime() {
        WMToast.normal("验证码已发送");
        getCodeIng = true;
        btnCode.setEnabled(false);
        //倒计时60s
        cd = new CountDownTimer(Const.MSG_COUNT_DOWN * 1000, 1000) {
            @Override
            public void onTick(long l) {
                btnCode.setText("重新发送(" + l / 1000 + "s)");
            }

            @Override
            public void onFinish() {
                endCountTime();
            }
        };
        cd.start();
    }

    /**
     * 结束倒计时
     */
    private void endCountTime() {
        if (cd != null) {
            cd.cancel();
            cd = null;
        }
        getCodeIng = false;
        if (btnCode == null)
            return;
        btnCode.setText("获取验证码");
        btnCode.setEnabled(true);
    }


    @Override
    public void onBackPressed() {
        endCountTime();
        super.onBackPressed();
    }
}
